package com.example.hotel_lxy.view.calendar;

import android.content.Context;

import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.bean.Bean;
import com.example.hotel_lxy.other.GroupItemDecoration;
import com.example.hotel_lxy.other.GroupRecyclerView;
import com.gavin.com.library.StickyDecoration;
import com.gavin.com.library.listener.GroupListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.L;

/**
 * Created by maning on 2017/5/10.
 * 垂直方向的日历
 */

public class MNCalendarVertical extends LinearLayout {

    private Context context;

    private RecyclerView recyclerViewCalendar;
    private LinearLayout ll_week;
    private TextView tv_week_01;
    private TextView tv_week_02;
    private TextView tv_week_03;
    private TextView tv_week_04;
    private TextView tv_week_05;
    private TextView tv_week_06;
    private TextView tv_week_07;

    private Calendar currentCalendar = Calendar.getInstance();

    private MNCalendarVerticalConfig mnCalendarVerticalConfig = new MNCalendarVerticalConfig.Builder().build();
    private MNCalendarVerticalAdapter mnCalendarVerticalAdapter;
    private List<Bean> mbean;
    private HashMap<String, ArrayList<MNCalendarItemModel>> dataMap;

    private OnCalendarRangeChooseListener onCalendarRangeChooseListener;

    public MNCalendarVertical(Context context) {
        this(context, null);
    }

    public MNCalendarVertical(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MNCalendarVertical(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {

        initViews();

        initCalendarDatas();

    }


    private void initViews() {
        //绑定View
        View.inflate(context, R.layout.mn_layout_calendar_vertical, this);
        recyclerViewCalendar = findViewById(R.id.recyclerViewCalendar);
        ll_week =  findViewById(R.id.ll_week);
        tv_week_01 =  findViewById(R.id.tv_week_01);
        tv_week_02 =  findViewById(R.id.tv_week_02);
        tv_week_03 = findViewById(R.id.tv_week_03);
        tv_week_04 =  findViewById(R.id.tv_week_04);
        tv_week_05 = findViewById(R.id.tv_week_05);
        tv_week_06 =  findViewById(R.id.tv_week_06);
        tv_week_07 =  findViewById(R.id.tv_week_07);

        //初始化RecycleerView
        recyclerViewCalendar.setLayoutManager(new LinearLayoutManager(context));
        recyclerViewCalendar.addItemDecoration(new GroupItemDecoration<String, MNCalendarItemModel>());
    }


    private void initCalendarDatas() {

        //星期栏的显示和隐藏
        boolean mnCalendar_showWeek = mnCalendarVerticalConfig.isMnCalendar_showWeek();
        if (mnCalendar_showWeek) {
            ll_week.setVisibility(View.VISIBLE);
            tv_week_01.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorWeek());
            tv_week_02.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorWeek());
            tv_week_03.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorWeek());
            tv_week_04.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorWeek());
            tv_week_05.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorWeek());
            tv_week_06.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorWeek());
            tv_week_07.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorWeek());
        } else {
            ll_week.setVisibility(View.GONE);
        }

        //日期集合
        dataMap = new HashMap<>();
        mbean=new ArrayList<>();
        //计算日期
        int mnCalendar_countMonth = mnCalendarVerticalConfig.getMnCalendar_countMonth();
        //需要在子线程中处理的逻辑
        for (int i = mnCalendar_countMonth/2; i >0; i--) {
                    int count7 = 0;
                    ArrayList<MNCalendarItemModel> mDatas = new ArrayList<>();
                    Calendar calendar = (Calendar) currentCalendar.clone();
                    calendar.add(Calendar.MONTH, -i);
                    //至于当月的第一天
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    //获取当月第一天是星期几
                    int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                    //移动到需要绘制的第一天
                    calendar.add(Calendar.DAY_OF_MONTH, -day_of_week);
                    //6*7
                    while (mDatas.size() < 6 * 7) {
                        Date date = calendar.getTime();
                        //阴历计算
                        Lunar lunar = LunarCalendarUtils.solarToLunar(date);
                        mDatas.add(new MNCalendarItemModel(date,lunar));
                        //包含两个7就多了一行
                        if (String.valueOf(calendar.getTime().getDate()).equals("7")) {
                            count7++;
                        }
                        //向前移动一天
                        calendar.add(Calendar.DAY_OF_MONTH, 1);
                    }
                    if (count7 >= 2) {
                        ArrayList<MNCalendarItemModel> mDatas2 = new ArrayList<>();
                        for (int j = 0; j < mDatas.size() - 7; j++) {
                            mDatas2.add(mDatas.get(j));
                        }
                        mDatas = new ArrayList<>();
                        mDatas.addAll(mDatas2);
                    }
                    Bean bean=new Bean();
                    bean.setTitle(-i);
                    bean.setMlist(mDatas);
                    mbean.add(bean);
                }

        //需要在子线程中处理的逻辑
        for (int i = 0; i < mnCalendar_countMonth/2; i++) {
                    int count7 = 0;
                    ArrayList<MNCalendarItemModel> mDatas = new ArrayList<>();
                    Calendar calendar = (Calendar) currentCalendar.clone();

                    calendar.add(Calendar.MONTH, i);
                    //至于当月的第一天
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    //获取当月第一天是星期几
                    int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                    //移动到需要绘制的第一天
                    calendar.add(Calendar.DAY_OF_MONTH, -day_of_week);
                    //6*7
                    while (mDatas.size() < 6 * 7) {
                        Date date = calendar.getTime();
                        //阴历计算
                        Lunar lunar = LunarCalendarUtils.solarToLunar(date);
                        mDatas.add(new MNCalendarItemModel(date,lunar));
                        //包含两个7就多了一行
                        if (String.valueOf(calendar.getTime().getDate()).equals("7")) {
                            count7++;
                        }
                        //向前移动一天
                        calendar.add(Calendar.DAY_OF_MONTH, 1);
                    }
                    if (count7 >= 2) {
                        ArrayList<MNCalendarItemModel> mDatas2 = new ArrayList<>();
                        for (int j = 0; j < mDatas.size() - 7; j++) {
                            mDatas2.add(mDatas.get(j));
                        }
                        mDatas = new ArrayList<>();
                        mDatas.addAll(mDatas2);
                    }
                    Bean bean=new Bean();
                    bean.setTitle(i);
                    bean.setMlist(mDatas);
                    mbean.add(bean);
//            dataMap.put(String.valueOf(i), mDatas);
                }

        //设置Adapter
        initAdapter();
    }

    private void initAdapter() {
        if (mnCalendarVerticalAdapter == null) {
            mnCalendarVerticalAdapter = new MNCalendarVerticalAdapter(context, mbean, currentCalendar, mnCalendarVerticalConfig);
            GroupListener groupListener = new GroupListener() {
                @Override
                public String getGroupName(int position) {
                    //获取分组名
                    //标题
                    Calendar calendarTitle = (Calendar) currentCalendar.clone();
                    calendarTitle.add(Calendar.MONTH, mbean.get(position).getTitle());
                    Date titleDate = calendarTitle.getTime();

                    //设置标题的格式
                    String mnCalendar_titleFormat = mnCalendarVerticalConfig.getMnCalendar_titleFormat();
                    SimpleDateFormat sdf = new SimpleDateFormat(mnCalendar_titleFormat);
                    return sdf.format(titleDate);
                }
            };
            StickyDecoration decoration = StickyDecoration.Builder
                    .init(groupListener).setGroupBackground(context.getResources().getColor(R.color.color_e9e8e8)).setGroupTextColor(context.getResources().getColor(R.color.color_020202)).build();
                    //重置span（使用GridLayoutManager时必须调用）
                    //.resetSpan(mRecyclerView, (GridLayoutManager) manager)
                   ;
            recyclerViewCalendar.addItemDecoration(decoration);
            recyclerViewCalendar.setAdapter(mnCalendarVerticalAdapter);
            recyclerViewCalendar.scrollToPosition(200);
        } else {
            mnCalendarVerticalAdapter.updateDatas(mbean, currentCalendar, mnCalendarVerticalConfig);
        }
        if (onCalendarRangeChooseListener != null) {
            mnCalendarVerticalAdapter.setOnCalendarRangeChooseListener(onCalendarRangeChooseListener);
        }
    }

    /**
     * 设置配置文件
     *
     * @param config
     */
    public void setConfig(MNCalendarVerticalConfig config) {
        this.mnCalendarVerticalConfig = config;
        initCalendarDatas();
    }


    public void setOnCalendarRangeChooseListener(OnCalendarRangeChooseListener onCalendarRangeChooseListener) {
        this.onCalendarRangeChooseListener = onCalendarRangeChooseListener;
        if (mnCalendarVerticalAdapter != null) {
            mnCalendarVerticalAdapter.setOnCalendarRangeChooseListener(onCalendarRangeChooseListener);
        }
    }

}
